package common.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.aspectj.util.FileUtil;

import common.exception.BusinessException;
import common.service.IStorageService;
import common.util.Constants;

public class StorageService implements IStorageService
{
	public static String BASE_FILE_DIR;
	private static String sep = Constants.fileSep;
	
	static
	{
		String catalina_home = System.getProperty("catalina.home");
		
		BASE_FILE_DIR = catalina_home+sep+"webapps"+sep+Constants.StorageDirName+sep;
	}

	@Override
	public String store(File file, String path, String fileName) throws BusinessException
	{
		if(file != null && file.exists())
		{
			String dir = checkDir(path);// path
			
			//file link: path/fileName.postfix
			String fileLink = dir+sep+fileName;
			File destFile = new File(BASE_FILE_DIR + fileLink);
			
			if(destFile.exists()){
				throw new BusinessException("The dest file has already exist, can't be overwrited.");
			}
			
			try
			{
				FileUtil.copyFile(file, destFile);
			} catch (IOException e)
			{
				throw new BusinessException(e);
			}
			
			return fileLink;
		}
		else
		{
			throw new BusinessException("Target file is null or not exist!");
		}
	}

	@Override
	public Map<String, File> batchGet(List<String> fileLinks)
			throws BusinessException
	{
		if(fileLinks != null && fileLinks.size() > 0)
		{
			Map<String, File> map = new HashMap<String, File>();
			for(String fileLink : fileLinks)
			{
				File file = get(fileLink);
				if(file != null && file.exists()) map.put(fileLink, file);
			}
			
			return map;
		}
		return null;
	}

	@Override
	public File get(String fileLink) throws BusinessException
	{
		if(StringUtils.isNotBlank(fileLink))
		{
			File file = new File(BASE_FILE_DIR+fileLink);
			if(file.exists()) return file;
		}
		else
		{
			throw new IllegalArgumentException("invalid fileLink["+fileLink+"]");
		}
		return null;
	}

	@Override
	public void remove(String fileLink) throws BusinessException
	{
		if(StringUtils.isNotBlank(fileLink))
		{
			remove(Arrays.asList(new String[]{fileLink}));
		}
	}

	@Override
	public void remove(List<String> fileLinks) throws BusinessException
	{
		if(fileLinks != null && fileLinks.size() > 0)
		{
			try
			{
				for(String fileLink : fileLinks)
				{
					File file = new File(BASE_FILE_DIR+fileLink);
					if(file.exists())  file.delete();
				}
			}
			catch(Exception e)
			{
				new BusinessException(e);
			}
		}
	}
	
	/**
	 * check if the given path do exist, if doesn't exist, then create it.
	 * @param path given directory path
	 * @return path without prefix separator and postfix separator, such as
	 * "dir1/dir2/dir3", "dir1/dir2" or "dir"
	 */
	private static String checkDir(String path)
	{
		if(StringUtils.isNotBlank(path))
		{
			StringBuilder sb = new StringBuilder(path);
			if(sep.equals(""+sb.charAt(0))) sb.deleteCharAt(0);
			if(sep.equals(""+sb.charAt(sb.length()-1))) sb.deleteCharAt(sb.length() -1);
			
			String location = BASE_FILE_DIR+sb.toString()+sep;
			
			File dir = new File(location);
			if(!dir.exists())
			{
				dir.mkdirs();
			}
			
			return sb.toString();
		}
		else
		{
			throw new IllegalArgumentException("path is null or blank. path=["+path+"]");
		}
	}
}
